/**
 * 重置
 */
var resetDownload = function(){
	$("#downloadHtmlForm").form("clear");
	
	var nowTime = getNowTime();
	
	$("#htmlName").val(nowTime);
	$("#encoding").combobox("setValue","UTF-8");
	
	$("#downloadHtmlForm").form("validate");
}

/**
 * 提交
 */
var submitDownload = function(){
	
	if($("#downloadHtmlForm").form("validate")){
		$("#downloadHtmlForm").form("submit", {
			url:"publish_downloadNewHtml",
			onSubmit: function(){
				maskOnLoad("正在下载页面，请稍等...");
			},
			success:function(data){
				var resultJson = $.parseJSON(data);
				
				closeMask();
				console.log(resultJson);
				console.log("##" + resultJson.error);
				if(resultJson.success != null){
					$("#newDownloadWin").window("close");
					$("#htmlListTab").datagrid("reload");
					$.messager.alert("成功",resultJson.success);
					
				}else{
					$.messager.alert("错误",resultJson.error);
				}
			}
		});
	}else{
		$.messager.alert("警告","输入有误！");
	}
	
	
}

/**
 * 查找窗口
 */
$(document).ready(function(){
	
	/**
	 * 添加新数据窗口打开
	 */
	var downloadNewHtml = function(){
		$("#newDownloadWin").window("open");
		resetDownload();
	}
	
	/**
	 * 拼接选择的行的id json
	 */
	var getSelectJson = function(){
		var selectRows = $("#htmlListTab").datagrid("getSelections");
		
		var jsonData = {};
		
		for(var i =0;i<selectRows.length;i++){
			jsonData["folderNameList[" + i + "].folderName"] = selectRows[i].folderName;
		}
		return jsonData;
	}
	
	/**
	 * 删除
	 */
	var deleteRow = function(){
		var selectRows = $("#htmlListTab").datagrid("getSelections");
		if(selectRows.length == 0){
			$.messager.alert("警告","请选择要删除的行！");
		}else{
			$.messager.confirm("警告", "删除之后不可恢复，确定要删除吗？",function(r){
				if (r){
					maskOnLoad("正在删除，请稍后。。。");
					var jsonData = getSelectJson();
					
					console.log(jsonData);
					
					$.ajax({
						type : "POST",
						dataType : "json",
						url : "publish_deleteDownloadHtml",
						data: jsonData,
						success : function(data){
							closeMask();
							if(data.success != null){
								$.messager.alert("成功","删除成功！");
								$("#htmlListTab").datagrid("reload");
							}else{
								$.messager.alert("失败",data.error);
							}
						},
						error : function(XMLHttpRequest, textStatus,errorThrown){
							closeMask();
							$.messager.alert("警告","删除失败！");
						}
					});
				}
			});
		}
	}
	
	/**
	 * 新增窗口
	 */
	$("#newDownloadWin").window({
		title : "下载新页面",
		collapsible: false,
		minimizable: false,
		closed: true,
		modal : true,
		top: 80,
		icon: "icon-save",
		onOpen: function(){
			//initInsertGrid("insertTab");
			//resetInsertGrid();
		}
	});
	
	/**
	 * 编码下拉框
	 * @return {TypeName} 
	 */
	$("#encoding").combobox({
		//url : "combobox_data.json",
		valueField : "encoding",
		textField : "encoding",
		panelHeight: "auto",
		editable: false,
		data: [{
			encoding: "UTF-8"
		},{
			encoding: "GBK"
		},{
			encoding: "GB2312"
		}]
	});
	
	/**
	 * 表单验证
	 */
	$.extend($.fn.validatebox.defaults.rules, {
		htmlNameVal : {
			validator : function(value, param) {
				var patt1 = new RegExp("^\\w{4,18}$");
				var flag = patt1.test(value);
				console.log(flag);
				return flag;
			},
			message : "名称必须为4-18位的英文、数字或下划线"
		}
	});
	
	$("#downloadUrl").validatebox({
		required: true,
		validType: "url"
	});
	$("#htmlName").validatebox({
		required: true,
		validType: "htmlNameVal"
	});
	
	/**
	 * 按钮
	 * @return {TypeName} 
	 */
	$("#btnReset").linkbutton({
		iconCls: "icon-cancel"
	});
	$("#btnSubmit").linkbutton({
		iconCls: "icon-ok"
	});
	
	
	/**
	 * 初始化表格
	 */
	$("#htmlListTab").datagrid({
		title:"下载页面列表",
		width: "auto",
		height: "auto",
		striped: true,	//斑马线
		pageSize : 50,
		pageNumber: 1,
		pageList:[30,50,100],
		rownumbers: true,
		singleSelect: false,	//是否单选
		url: "publish_getPublishHtml",
		pagination: true,	//分页
		frozenColumns : [[	//冻结列
			{field : "ck",checkbox : true}//复选框
		]],
		columns : [[
			{
				field: "folderName",
				title: "页面名称",
				width: 500,
				rowspan: 2,
				formatter: function(value,row,index){
					return "<a href='downloadHtml/" + value + "/index.html' target='_blank'>" + value + "</a>";
            	}
			}
		]],//列
		//queryParams: {
		//	"queryAttr.fieldName": "",
		//	"queryAttr.fieldType": "",
		//	"queryAttr.keyword": ""
		//},
		toolbar:[{
			text:"添加",
			iconCls:"icon-add",
			handler:function(){
				downloadNewHtml();
			}
		},{
			text:"删除",
			iconCls:"icon-cancel",
			handler:function(){
				deleteRow();
			}
		}]
	});
});


